package org.jeecg.modules.system.szty;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.*;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;

public class SztyServes {
    private static final String HMAC_SHA1_ALGORITHM = "HmacSHA1";

    /**
     * serverToken 获取服务口令
     * @return
     * @throws NoSuchAlgorithmException
     * @throws InvalidKeyException
     * @throws IOException
     */
    public String getTokenServe() throws NoSuchAlgorithmException, InvalidKeyException, IOException {
        //随机码
        String sss = "EWfADbCnR8kxirMB115qIk3T4DHSNtsT";
        //key
        String key = "TFJUBrMwCyVsQnsJHFZPVjWFvk1HdruQ";
        //时间戳
        String timestamp = Long.toString(System.currentTimeMillis());
        //原数据
        String data = sss+timestamp;
        SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), HMAC_SHA1_ALGORITHM);
        Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
        mac.init(signingKey);
        byte[] rawHmac = mac.doFinal(data.getBytes());
        StringBuilder hmacBuilder = new StringBuilder();
        for (byte b : rawHmac) {
            hmacBuilder.append(String.format("%02x", b));
        }
        //Authorization
        String Authorization = hmacBuilder.toString();
        //应用id
        String appCode = "507275147443437568";
        //随机码
        String signatureNonce = sss;
        //时间戳
        //String timestamp2 = timestamp;
        //String url ="http://11.89.24.203:9022/server_token/get";
        // 设置接口地址
        URL url = new URL("http://11.89.24.203:9022/server_token/get");
        // 设置POST参数
        Map<String, String> parameters = new HashMap<>();
        parameters.put("appCode", appCode);
        parameters.put("Authorization", Authorization);
        parameters.put("signatureNonce", signatureNonce);
        parameters.put("timestamp", timestamp);
        // 创建连接
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        connection.setDoOutput(true);
        // 发送POST请求
        try (OutputStream outputStream = connection.getOutputStream()) {
            StringBuilder postData = new StringBuilder();
            for (Map.Entry<String, String> parameter : parameters.entrySet()) {
                if (postData.length() != 0) postData.append('&');
                postData.append(URLEncoder.encode(parameter.getKey(), "UTF-8"));
                postData.append('=');
                postData.append(URLEncoder.encode(parameter.getValue(), "UTF-8"));
            }
            byte[] postDataBytes = postData.toString().getBytes("UTF-8");
            outputStream.write(postDataBytes);
        }
        // 读取响应  暂时没测，需要按实际情况返回数据
        StringBuilder response = new StringBuilder();
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
            String line;
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
            System.out.println(response.toString());
        }
        return response.toString();
    }

    /**
     * 获取用户口令 userToken
     * @param code 用户登录成功后返回的字符串
     * @param serverToken
     * @return
     * @throws IOException
     */
    public String getUserToken(String code,String serverToken) throws IOException {
        URL url = new URL("http://11.89.24.203:9022/login_api/get_user_token");
        // 设置POST参数
        Map<String, String> parameters = new HashMap<>();
        parameters.put("corpId", code);
        parameters.put("serverToken", serverToken);
        // 创建连接
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        connection.setDoOutput(true);
        // 发送POST请求
        try (OutputStream outputStream = connection.getOutputStream()) {
            StringBuilder postData = new StringBuilder();
            for (Map.Entry<String, String> parameter : parameters.entrySet()) {
                if (postData.length() != 0) postData.append('&');
                postData.append(URLEncoder.encode(parameter.getKey(), "UTF-8"));
                postData.append('=');
                postData.append(URLEncoder.encode(parameter.getValue(), "UTF-8"));
            }
            byte[] postDataBytes = postData.toString().getBytes("UTF-8");
            outputStream.write(postDataBytes);
        }
        // 读取响应  暂时没测，需要按实际情况返回数据
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
            String line;
            StringBuilder response = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
            System.out.println(response.toString());
        }
        return null;
    }


    /**
     *  获取登录用户信息
     *  获取登录用户信息，根据获取的用户口令获取用户的信息
     * @param userToken 用户口令
     * @param serverToken
     * @return
     * @throws IOException
     */
    public String getUserInfo(String userToken,String serverToken) throws IOException {
        URL url = new URL("http://11.89.24.203:9022/login_api/get_user_info");
        // 设置POST参数
        Map<String, String> parameters = new HashMap<>();
        parameters.put("userToken", userToken);
        parameters.put("serverToken", serverToken);
        // 创建连接
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        connection.setDoOutput(true);
        // 发送POST请求
        try (OutputStream outputStream = connection.getOutputStream()) {
            StringBuilder postData = new StringBuilder();
            for (Map.Entry<String, String> parameter : parameters.entrySet()) {
                if (postData.length() != 0) postData.append('&');
                postData.append(URLEncoder.encode(parameter.getKey(), "UTF-8"));
                postData.append('=');
                postData.append(URLEncoder.encode(parameter.getValue(), "UTF-8"));
            }
            byte[] postDataBytes = postData.toString().getBytes("UTF-8");
            outputStream.write(postDataBytes);
        }
        // 读取响应  暂时没测，需要按实际情况返回数据
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
            String line;
            StringBuilder response = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
            System.out.println(response.toString());
        }
        return null;
    }

    /**
     *获取全部单位
     * @param fieldKeyList
     * @param serverToken 服务器口令 必须要的参数
     * @return
     * @throws IOException
     */
    public String getListAllCorp(String fieldKeyList,String serverToken) throws IOException {
        URL url = new URL("http://11.89.24.203:9022/corp_api/list_all_corp");
        // 设置POST参数
        Map<String, String> parameters = new HashMap<>();
        parameters.put("serverToken", serverToken);
        // 创建连接
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.setDoOutput(true);
        // 发送POST请求
        try (OutputStream outputStream = connection.getOutputStream()) {
            StringBuilder postData = new StringBuilder();
            for (Map.Entry<String, String> parameter : parameters.entrySet()) {
                if (postData.length() != 0) postData.append('&');
                postData.append(URLEncoder.encode(parameter.getKey(), "UTF-8"));
                postData.append('=');
                postData.append(URLEncoder.encode(parameter.getValue(), "UTF-8"));
            }
            byte[] postDataBytes = postData.toString().getBytes("UTF-8");
            outputStream.write(postDataBytes);
        }
        // 读取响应  暂时没测，需要按实际情况返回数据
        StringBuilder response = new StringBuilder();
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
            String line;

            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
            System.out.println(response.toString());
        }
        return response.toString();
    }

    /**
     *获取员工列表
     * @param corpId 必需要 单位id,通过单位列表获取
     * @param departmentId
     * @param fieldKeyList
     * @param serverToken 必需要
     * @return
     * @throws IOException
     */
    public String getListAllEmployee(String corpId,String departmentId,String fieldKeyList,String serverToken) throws IOException {
        URL url = new URL("http://11.89.24.203:9022/employee_api/list_all_employee");
        // 设置POST参数
        Map<String, String> parameters = new HashMap<>();
        parameters.put("corpId", corpId);
        parameters.put("serverToken", serverToken);
        // 创建连接
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.setDoOutput(true);
        // 发送POST请求
        try (OutputStream outputStream = connection.getOutputStream()) {
            StringBuilder postData = new StringBuilder();
            for (Map.Entry<String, String> parameter : parameters.entrySet()) {
                if (postData.length() != 0) postData.append('&');
                postData.append(URLEncoder.encode(parameter.getKey(), "UTF-8"));
                postData.append('=');
                postData.append(URLEncoder.encode(parameter.getValue(), "UTF-8"));
            }
            byte[] postDataBytes = postData.toString().getBytes("UTF-8");
            outputStream.write(postDataBytes);
        }
        // 读取响应  暂时没测，需要按实际情况返回数据
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
            String line;
            StringBuilder response = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
            System.out.println(response.toString());
        }
        return null;
    }

    /**
     *获取单个员工
     * @param fieldKeyList
     * @param id 必须要 员工id，通过员工列表获取
     * @param serverToken 必须要
     * @return
     * @throws IOException
     */
    public String getEmployee(String fieldKeyList,String id,String serverToken) throws IOException {
        URL url = new URL("http://11.89.24.203:9022/employee_api/get");
        // 设置POST参数
        Map<String, String> parameters = new HashMap<>();
        parameters.put("corpId", id);
        parameters.put("serverToken", serverToken);
        // 创建连接
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.setDoOutput(true);
        // 发送POST请求
        try (OutputStream outputStream = connection.getOutputStream()) {
            StringBuilder postData = new StringBuilder();
            for (Map.Entry<String, String> parameter : parameters.entrySet()) {
                if (postData.length() != 0) postData.append('&');
                postData.append(URLEncoder.encode(parameter.getKey(), "UTF-8"));
                postData.append('=');
                postData.append(URLEncoder.encode(parameter.getValue(), "UTF-8"));
            }
            byte[] postDataBytes = postData.toString().getBytes("UTF-8");
            outputStream.write(postDataBytes);
        }
        // 读取响应  暂时没测，需要按实际情况返回数据
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
            String line;
            StringBuilder response = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
            System.out.println(response.toString());
        }
        return null;
    }

    /**
     *获取部门列表
     * @param corpId  必须要  单位id，通过单位列表获取，传空获取所有自由
     * @param fieldKeyList
     * @param serverToken 必须要
     * @return
     * @throws IOException
     */
    public String getListAllDepartment(String corpId,String fieldKeyList,String serverToken) throws IOException {
        URL url = new URL("http://11.89.24.203:9022/department_api/list_all_department");
        // 设置POST参数
        Map<String, String> parameters = new HashMap<>();
        parameters.put("corpId", corpId);
        parameters.put("serverToken", serverToken);
        // 创建连接
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.setDoOutput(true);
        // 发送POST请求
        try (OutputStream outputStream = connection.getOutputStream()) {
            StringBuilder postData = new StringBuilder();
            for (Map.Entry<String, String> parameter : parameters.entrySet()) {
                if (postData.length() != 0) postData.append('&');
                postData.append(URLEncoder.encode(parameter.getKey(), "UTF-8"));
                postData.append('=');
                postData.append(URLEncoder.encode(parameter.getValue(), "UTF-8"));
            }
            byte[] postDataBytes = postData.toString().getBytes("UTF-8");
            outputStream.write(postDataBytes);
        }
        // 读取响应  暂时没测，需要按实际情况返回数据
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
            String line;
            StringBuilder response = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
            System.out.println(response.toString());
        }
        return null;
    }
    /**
     *获取单个部门
     * @param fieldKeyList
     * @param id 部门id，通过部门列表获取
     * @param serverToken 获取职务列表
     * @return
     * @throws IOException
     */
    public String getDepartment(String fieldKeyList,String id,String serverToken) throws IOException {
        URL url = new URL("http://11.89.24.203:9022/department_api/get");
        // 设置POST参数
        Map<String, String> parameters = new HashMap<>();
        parameters.put("id", id);
        parameters.put("serverToken", serverToken);
        // 创建连接
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.setDoOutput(true);
        // 发送POST请求
        try (OutputStream outputStream = connection.getOutputStream()) {
            StringBuilder postData = new StringBuilder();
            for (Map.Entry<String, String> parameter : parameters.entrySet()) {
                if (postData.length() != 0) postData.append('&');
                postData.append(URLEncoder.encode(parameter.getKey(), "UTF-8"));
                postData.append('=');
                postData.append(URLEncoder.encode(parameter.getValue(), "UTF-8"));
            }
            byte[] postDataBytes = postData.toString().getBytes("UTF-8");
            outputStream.write(postDataBytes);
        }
        // 读取响应  暂时没测，需要按实际情况返回数据
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
            String line;
            StringBuilder response = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
            System.out.println(response.toString());
        }
        return null;
    }

    /**
     * 获取职务列表
     * @param corpId
     * @param departmentId
     * @param fieldKeyList
     * @param serverToken 必填
     * @return
     * @throws IOException
     */
    public String getListAllPosition(String corpId,String departmentId,String fieldKeyList,String serverToken) throws IOException {
        URL url = new URL("http://11.89.24.203:9022/position_api/list_all_position");
        // 设置POST参数
        Map<String, String> parameters = new HashMap<>();
        parameters.put("serverToken", serverToken);
        // 创建连接
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.setDoOutput(true);
        // 发送POST请求
        try (OutputStream outputStream = connection.getOutputStream()) {
            StringBuilder postData = new StringBuilder();
            for (Map.Entry<String, String> parameter : parameters.entrySet()) {
                if (postData.length() != 0) postData.append('&');
                postData.append(URLEncoder.encode(parameter.getKey(), "UTF-8"));
                postData.append('=');
                postData.append(URLEncoder.encode(parameter.getValue(), "UTF-8"));
            }
            byte[] postDataBytes = postData.toString().getBytes("UTF-8");
            outputStream.write(postDataBytes);
        }
        // 读取响应  暂时没测，需要按实际情况返回数据
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
            String line;
            StringBuilder response = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
            System.out.println(response.toString());
        }
        return null;
    }

    /**
     *
     * @param fieldKeyList
     * @param id 必填 职务id，通过职务列表获取
     * @param serverToken 必填
     * @return
     * @throws IOException
     */
    public String getPosition(String fieldKeyList,String id,String serverToken) throws IOException {
        URL url = new URL("http://11.89.24.203:9022/position_api/get");
        // 设置POST参数
        Map<String, String> parameters = new HashMap<>();
        parameters.put("id", id);
        parameters.put("serverToken", serverToken);
        // 创建连接
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.setDoOutput(true);
        // 发送POST请求
        try (OutputStream outputStream = connection.getOutputStream()) {
            StringBuilder postData = new StringBuilder();
            for (Map.Entry<String, String> parameter : parameters.entrySet()) {
                if (postData.length() != 0) postData.append('&');
                postData.append(URLEncoder.encode(parameter.getKey(), "UTF-8"));
                postData.append('=');
                postData.append(URLEncoder.encode(parameter.getValue(), "UTF-8"));
            }
            byte[] postDataBytes = postData.toString().getBytes("UTF-8");
            outputStream.write(postDataBytes);
        }
        // 读取响应  暂时没测，需要按实际情况返回数据
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
            String line;
            StringBuilder response = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
            System.out.println(response.toString());
        }
        return null;
    }

    /**
     * 获取指定职务下员工
     * @param fieldKeyList 必填 需要获取的员工信息字段的key，以逗号分隔的字符，为空时获取所有信息
     * @param positionId 必填 职务id
     * @param serverToken 必填 服务器口令
     * @return
     * @throws IOException
     */
    public String getListByPosition(String fieldKeyList,String positionId,String serverToken) throws IOException {
        URL url = new URL("http://11.89.24.203:9022/employee_api/list_by_position");
        // 设置POST参数
        Map<String, String> parameters = new HashMap<>();
        parameters.put("fieldKeyList", fieldKeyList);
        parameters.put("positionId", positionId);
        parameters.put("serverToken", serverToken);
        // 创建连接
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.setDoOutput(true);
        // 发送POST请求
        try (OutputStream outputStream = connection.getOutputStream()) {
            StringBuilder postData = new StringBuilder();
            for (Map.Entry<String, String> parameter : parameters.entrySet()) {
                if (postData.length() != 0) postData.append('&');
                postData.append(URLEncoder.encode(parameter.getKey(), "UTF-8"));
                postData.append('=');
                postData.append(URLEncoder.encode(parameter.getValue(), "UTF-8"));
            }
            byte[] postDataBytes = postData.toString().getBytes("UTF-8");
            outputStream.write(postDataBytes);
        }
        // 读取响应  暂时没测，需要按实际情况返回数据
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
            String line;
            StringBuilder response = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
            System.out.println(response.toString());
        }
        return null;
    }
}
